package com.deparse.dpframe.bean;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.deparse.dpframe.ui.view.DPTabItemContainer;
import com.deparse.dpframe.ui.view.DPTabItem;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by MartinDKent on 2016/1/16.
 */
public class DPView extends DPObject {
    private Object obj = null;

    private Map<Integer, DPView> cacher = new HashMap<>(20);

    public DPView(Object obj) {
        this.obj = obj;
    }

    public <T> T cast() {
        return null == obj ? null : (T) obj;
    }

    public boolean isEmpty() {
        return null == obj;
    }

    public boolean isView() {
        return obj instanceof View;
    }

    public boolean isTextView() {
        return obj instanceof TextView;
    }

    public boolean isImageView() {
        return obj instanceof ImageView;
    }

    public boolean isButton() {
        return obj instanceof Button;
    }

    public boolean isDPTabHost() {
        return obj instanceof DPTabItemContainer;
    }

    public boolean isDPTabWidget() {
        return obj instanceof DPTabItem;
    }

    public boolean isAbsListView() {
        return obj instanceof AbsListView;
    }

    public boolean isListView() {
        return obj instanceof ListView;
    }

    public boolean isGridView() {
        return obj instanceof GridView;
    }

    public boolean isLinearLayout() {
        return obj instanceof LinearLayout;
    }

    public boolean isRelativeLayout() {
        return obj instanceof RelativeLayout;
    }

    public boolean isFragmeLayout() {
        return obj instanceof FrameLayout;
    }

    public boolean isViewGroup() {
        return obj instanceof ViewGroup;
    }

    public boolean isAdapterView() {
        return obj instanceof AdapterView;
    }

    /**
     * @param resId
     * @param <T>
     * @return
     */
    public <T> T F(int resId) {
        if (isView()) {
            View view = (View) obj;
            return null == view.findViewById(resId) ? null : (T) view.findViewById(resId);
        }
        return null;
    }

    public DPView $(int resId) {
        if (null == cacher.get(resId)) {
            cacher.put(resId, new DPView(F(resId)));
        }
        return cacher.get(resId);
    }

    public String text() {
        if (isTextView()) {
            return ((TextView) obj).getText().toString();
        }
        return null;
    }

    public void text(int resId) {
        if (isTextView()) {
            ((TextView) obj).setText(resId);
        }
    }

    public void text(String text) {
        if (isTextView()) {
            ((TextView) obj).setText(text);
        }
    }

    public void src(int resId) {
        if (isImageView()) {
            ((ImageView) obj).setImageResource(resId);
        }
    }

    public void background(int resId) {
        if (isView()) {
            ((View) obj).setBackgroundResource(resId);
        }
    }

    public void enable() {
        if (isView()) {
            ((View) obj).setEnabled(true);
        }
    }

    public void disable() {
        if (isView()) {
            ((View) obj).setEnabled(false);
        }
    }

    public boolean isEnabled() {
        if (isView()) {
            return ((View) obj).isEnabled();
        }
        return false;
    }

    public void click(final View.OnClickListener listener) {
        if (isView()) {
            ((View) obj).setOnClickListener(listener);
        }
    }

    public void tabClick(final DPTabItemContainer.OnTabClickListener listener) {
        if (isDPTabHost()) {
            ((DPTabItemContainer) obj).setOnTabClickListener(listener);
        }
    }

    public void tabChange(final DPTabItemContainer.OnTabChangeListener listener) {
        if (isDPTabHost()) {
            ((DPTabItemContainer) obj).setOnTabChangeListener(listener);
        }
    }

    public void touch(final View.OnTouchListener listener) {
        if (isView()) {
            ((View) obj).setOnTouchListener(listener);
        }
    }

    public void itemClick(final AdapterView.OnItemClickListener listener) {
        if (isAbsListView()) {
            ((AbsListView) obj).setOnItemClickListener(listener);
        }
    }

    public void itemLongClick(final AdapterView.OnItemLongClickListener listener) {
        if (isAbsListView()) {
            ((AbsListView) obj).setOnItemLongClickListener(listener);
        }
    }

    public void scroll(final AbsListView.OnScrollListener listener) {
        if (isAbsListView()) {
            ((AbsListView) obj).setOnScrollListener(listener);
        }
    }

    public void adapter(Adapter baseAdapter) {
        if (isAdapterView()) {
            ((AdapterView) obj).setAdapter(baseAdapter);
        }
    }

    public void hide() {
        if (isView()) {
            ((View) obj).setVisibility(View.GONE);
        }
    }

    public void show() {
        if (isView()) {
            ((View) obj).setVisibility(View.VISIBLE);
        }
    }
}
